home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
8352
/
8352.xpi
/
components
/
gfGreasefireService.js
next >
Wrap
Text File
|
2009-05-24
|
22KB
|
760 lines
/*
* Copyright (C) 2008 by Steve Krulewitz <skrulx@gmail.com>
* Licensed under GPLv2 or later, see file LICENSE in the xpi for details.
*/
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
Components.utils.import("resource://gre/modules/ISO8601DateUtils.jsm");
const DEBUG = false;
const NS_PROFILE_STARTUP_OBSERVER_ID = "profile-after-change";
const NS_PROFILE_SHUTDOWN_OBSERVER_ID = "profile-before-change";
function GF_GetIndexesDir() {
var em = Cc["@mozilla.org/extensions/manager;1"]
.getService(Ci.nsIExtensionManager);
var installLocation = em.getInstallLocation("greasefire@skrul.com");
var file = installLocation.location;
file.append("greasefire@skrul.com");
file.append("indexes");
return file;
}
function d(s) {
if (DEBUG) {
dump("gfGreasefireService: " + s + "\n");
}
}
function gfGreasefireService()
{
d("ctor");
this._started = false;
this._includes = null;
this._excludes = null;
this._conn = null;
this._scriptCount = null;
this._indexDate = Date.now();
var obs = Cc["@mozilla.org/observer-service;1"]
.getService(Ci.nsIObserverService);
obs.addObserver(this, NS_PROFILE_STARTUP_OBSERVER_ID, false);
obs.addObserver(this, NS_PROFILE_SHUTDOWN_OBSERVER_ID, false);
}
gfGreasefireService.prototype = {
classDescription: "Greasefire Service",
classID: Components.ID("{d647ff9b-ac4c-4d0e-8fbd-484765be5549}"),
contractID: "@skrul.com/greasefire/service;1"
}
gfGreasefireService.prototype.startup =
function gfGreasefirbeService_startup()
{
d("startup");
if (this._started) {
return;
}
this._scriptCount = null;
this._indexDate = null;
var dir = GF_GetIndexesDir();
var file = dir.clone();
file.append("include.dat");
this._includes = new gfIndexReader(file);
var file = dir.clone();
file.append("exclude.dat");
this._excludes = new gfIndexReader(file);
var storageService = Cc["@mozilla.org/storage/service;1"]
.getService(Ci.mozIStorageService);
file = dir.clone();
file.append("scripts.db");
this._conn = storageService.openDatabase(file);
this._started = true;
}
gfGreasefireService.prototype.shutdown =
function gfGreasefireService_shutdown()
{
d("shutdown");
if (!this._started) {
return;
}
if (this._includes) {
this._includes.close();
}
if (this._excludes) {
this._excludes.close();
}
// Only mozilla 1.9 has a close method
if (this._conn && this._conn.close) {
this._conn.close();
}
this._started = false;
}
// gfIGreasefireService
gfGreasefireService.prototype.hasScripts =
function gfGreasefireService_hasScripts(aURL)
{
var urlSpec = this._fixUrl(aURL);
var excludes = {};
this._excludes.search(urlSpec, excludes, false);
var matches = {};
this._includes.search(urlSpec, matches, true, excludes);
for (var id in matches) {
return true;
}
return false;
}
gfGreasefireService.prototype.search =
function gfGreasefireService_search(aURL)
{
var urlSpec = this._fixUrl(aURL);
var excludes = {};
var t = Date.now();
this._excludes.search(urlSpec, excludes, false);
var excludesMark = Date.now();
var matches = {};
this._includes.search(urlSpec, matches, false, excludes);
var searchMark = Date.now();
var infos = this._getScriptInfos(matches);
var ranks = this._rankMatches(matches, infos);
var result = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
for (var id in matches) {
var r = new gfSearchResult(id, infos[id], matches[id], ranks[id]);
result.appendElement(r, false);
}
d("****** Search " + aURL.spec +
" matches " + result.length +
" exclude " + (excludesMark - t) +
" include " + (searchMark - t));
return result;
}
gfGreasefireService.prototype.__defineGetter__("scriptCount",
function gfGreasefireService_get_scriptCount()
{
if (!this._started) {
throw Cr.NS_ERROR_UNEXPECTED;
}
if (!this._scriptCount) {
var sql = "select count(1) from scripts";
var stmt = this._conn.createStatement(sql);
stmt.executeStep();
this._scriptCount = stmt.getInt32(0);
stmt.finalize();
}
return this._scriptCount;
});
gfGreasefireService.prototype.__defineGetter__("indexDate",
function gfGreasefireService_get_indexDate()
{
if (!this._indexDate) {
this._indexDate = 0;
var iniFile = GF_GetIndexesDir();
iniFile.append("info.ini");
if (iniFile.exists()) {
try {
var ini = Cc["@mozilla.org/xpcom/ini-parser-factory;1"]
.getService(Ci.nsIINIParserFactory).createINIParser(iniFile);
var dateString = ini.getString("indexes", "date");
this._indexDate = ISO8601DateUtils.parse(dateString).getTime();
}
catch (e) {
Cu.reportError(e);
}
}
}
return this._indexDate;
});
gfGreasefireService.prototype._fixUrl =
function gfGreasefireService__fixUrl(aURL)
{
var urlSpec = aURL.spec;
// XXX performance breaks down on really long URLs, so trim to 50 chars
urlSpec = urlSpec.substring(0, 50);
return urlSpec;
}
gfGreasefireService.prototype._getScriptInfos =
function gfGreasefireService__getSctiptInfos(matches)
{
var ids = [];
for (var id in matches) {
ids.push(id);
}
var infos = {};
var sql = "select id, name, installs, updated from scripts where id in (";
sql += ids.join(",");
sql += ");";
var stmt = this._conn.createStatement(sql);
while (stmt.executeStep()) {
infos[stmt.getInt32(0)] = {
name: stmt.getString(1),
installs: stmt.getInt32(2),
updated: stmt.getInt64(3)
}
}
stmt.finalize();
return infos;
}
gfGreasefireService.prototype._rankMatches =
function gfGreasefireService__rankMatches(matches, infos)
{
var ranks = [];
var updatedMin = null;
var updatedMax = null;
var installsMin = null;
var installsMax = null;
var matchMin = null;
var matchMax = null;
for (var id in matches) {
var info = infos[id];
var match = matches[id];
if (updatedMin == null || info.updated < updatedMin) {
updatedMin = info.updated;
}
if (updatedMax == null || info.updated > updatedMax) {
updatedMax = info.updated;
}
if (installsMin == null || info.installs < installsMin) {
installsMin = info.installs;
}
if (installsMax == null || info.installs > installsMax) {
installsMax = info.installs;
}
if (matchMin == null || match < matchMin) {
matchMin = match;
}
if (matchMax == null || match > matchMax) {
matchMax = match;
}
}
var updatedRange = updatedMax - updatedMin;
var installsRange = installsMax - installsMin;
var matchRange = matchMax - matchMin;
for (var id in matches) {
var info = infos[id];
var matchCount = matches[id];
var updated = updatedRange > 0 ?
(info.updated - updatedMin) / updatedRange : 1;
var installs = installsRange > 0 ?
(info.installs - installsMin) / installsRange : 1;
var match = matchRange > 0 ?
(matchCount - matchMin) / matchRange : 1;
ranks[id] = (updated * .5) + (installs * .25) + (match * .25);
}
return ranks;
}
// nsIObserver
gfGreasefireService.prototype.observe =
function gfGreasefireService_observe(aSubject, aTopic, aData)
{
if (aTopic == NS_PROFILE_STARTUP_OBSERVER_ID) {
this.startup();
}
else if (aTopic == NS_PROFILE_SHUTDOWN_OBSERVER_ID) {
this.shutdown();
var obs = Cc["@mozilla.org/observer-service;1"]
.getService(Ci.nsIObserverService);
obs.removeObserver(this, NS_PROFILE_STARTUP_OBSERVER_ID);
obs.removeObserver(this, NS_PROFILE_SHUTDOWN_OBSERVER_ID);
}
}
function gfSearchResult(aScriptId, aInfo, aMatch, aRank)
{
this._scriptId = aScriptId;
this._info = aInfo;
this._match = aMatch;
this._rank = aRank;
}
gfSearchResult.prototype = {
get scriptId() {
return this._scriptId;
},
get match() {
return this._match;
},
get name() {
return this._info.name;
},
get installs() {
return this._info.installs;
},
get updated() {
return this._info.updated;
},
get rank() {
return this._rank;
}
}
function gfIndexReader(aFile)
{
this._fis = Cc["@mozilla.org/network/file-input-stream;1"]
.createInstance(Ci.nsIFileInputStream);
this._fis.init(aFile, -1, 0, 0);
var buffer = Cc["@mozilla.org/network/buffered-input-stream;1"]
.createInstance(Ci.nsIBufferedInputStream);
buffer.init(this._fis, 4096);
this._seekable = buffer.QueryInterface(Ci.nsISeekableStream);
this._bis = Cc["@mozilla.org/binaryinputstream;1"]
.createInstance(Ci.nsIBinaryInputStream);
this._bis.setInputStream(buffer);
this._cache = [];
}
gfIndexReader.prototype = {
close: function() {
this._fis.close();
},
search: function(aUrl, aMatches, aFirstOnly, aExcludes) {
this._search(aUrl, 0, 0, 0, aMatches, aFirstOnly, aExcludes);
},
_search: function(aUrl, aIndexPos, aStringPos, aMatchedCount, aMatches, aFirstOnly, aExcludes) {
var o = this._cache[aIndexPos];
if (!o) {
o = {};
this._cache[aIndexPos] = o;
this._seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, aIndexPos);
var idsLength = this._bis.read64();
if (idsLength > 0) {
var ids = [];
for (var i = 0; i < idsLength; i++) {
ids.push(this._bis.read64());
}
o["ids"] = ids;
}
var childrenLength = this._bis.read16();
for (var i = 0; i < childrenLength; i++) {
var c = String.fromCharCode(this._bis.read16());
var pos = this._bis.read64();
o[c] = pos;
}
}
var ids = o["ids"];
if (ids) {
for (var i = 0; i < ids.length; i++) {
var id = ids[i];
var matchedCount = aMatches[id];
if (!matchedCount || (matchedCount && aMatchedCount > matchedCount)) {
if (!aExcludes || (aExcludes && !(id in aExcludes))) {
aMatches[id] = aMatchedCount;
if (aFirstOnly) {
return false;
}
}
}
}
}
var wildPos = o["*"];
if (wildPos) {
for (var i = 0; i < aUrl.length; i++) {
var cont = this._search(aUrl,
wildPos,
i,
aMatchedCount,
aMatches,
aFirstOnly,
aExcludes);
if(!cont) {
return false;
}
}
}
if (aStringPos > aUrl.length - 1) {
return true;
}
var nextPos = o[aUrl.charAt(aStringPos)];
if (nextPos) {
var cont = this._search(aUrl,
nextPos,
aStringPos + 1,
aMatchedCount + 1,
aMatches,
aFirstOnly,
aExcludes);
if(!cont) {
return false;
}
}
var tldPos = o[" "];
if (tldPos) {
// Consume all characters up to /
var slashPos = aUrl.indexOf("/", aStringPos + 1);
if (slashPos >= 0) {
var cont = this._search(aUrl,
tldPos,
slashPos,
aMatchedCount + 1,
aMatches,
aFirstOnly,
aExcludes);
if(!cont) {
return false;
}
}
}
return true;
}
}
/*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2004
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Alex Fritze <alex@croczilla.com> (original author)
* Nickolay Ponomarev <asqueella@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/**
* Utilities for JavaScript components loaded by the JS component
* loader.
*
* Import into a JS component using
* 'Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");'
*
* Exposing a JS 'class' as a component using these utility methods consists
* of several steps:
* 0. Import XPCOMUtils, as described above.
* 1. Declare the 'class' (or multiple classes) implementing the component(s):
* function MyComponent() {
* // constructor
* }
* MyComponent.prototype = {
* // properties required for XPCOM registration:
* classDescription: "unique text description",
* classID: Components.ID("{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"),
* contractID: "@example.com/xxx;1",
*
* // [optional] custom factory (an object implementing nsIFactory). If not
* // provided, the default factory is used, which returns
* // |(new MyComponent()).QueryInterface(iid)| in its createInstance().
* _xpcom_factory: { ... },
*
* // [optional] an array of categories to register this component in.
* _xpcom_categories: [{
* // Each object in the array specifies the parameters to pass to
* // nsICategoryManager.addCategoryEntry(). 'true' is passed for
* // both aPersist and aReplace params.
* category: "some-category",
* // optional, defaults to the object's classDescription
* entry: "entry name",
* // optional, defaults to the object's contractID (unless
* // 'service' is specified)
* value: "...",
* // optional, defaults to false. When set to true, and only if 'value'
* // is not specified, the concatenation of the string "service," and the
* // object's contractID is passed as aValue parameter of addCategoryEntry.
* service: true
* }],
*
* // QueryInterface implementation, e.g. using the generateQI helper
* QueryInterface: XPCOMUtils.generateQI(
* [Components.interfaces.nsIObserver,
* Components.interfaces.nsIMyInterface]),
*
* // ...component implementation...
* };
*
* 2. Create an array of component constructors (like the one
* created in step 1):
* var components = [MyComponent];
*
* 3. Define the NSGetModule entry point:
* function NSGetModule(compMgr, fileSpec) {
* // components is the array created in step 2.
* return XPCOMUtils.generateModule(components);
* }
*/
var XPCOMUtils = {
/**
* Generate a QueryInterface implementation. The returned function must be
* assigned to the 'QueryInterface' property of a JS object. When invoked on
* that object, it checks if the given iid is listed in the |interfaces|
* param, and if it is, returns |this| (the object it was called on).
*/
generateQI: function(interfaces) {
return makeQI([i.name for each(i in interfaces)]);
},
/**
* Generate the NSGetModule function (along with the module definition).
* See the parameters to generateModule.
*/
generateNSGetModule: function(componentsArray, postRegister, preUnregister) {
return function NSGetModule(compMgr, fileSpec) {
return XPCOMUtils.generateModule(componentsArray,
postRegister,
preUnregister);
}
},
/**
* Generate a module implementation.
*
* @param componentsArray Array of component constructors. See the comment
* at the top of this file for details.
* @param postRegister optional post-registration function with
* signature 'postRegister(nsIComponentManager,
* nsIFile, componentsArray)'
* @param preUnregister optional pre-unregistration function with
* signature 'preUnregister(nsIComponentManager,
* nsIFile, componentsArray)'
*/
generateModule: function(componentsArray, postRegister, preUnregister) {
let classes = [];
for each (let component in componentsArray) {
classes.push({
cid: component.prototype.classID,
className: component.prototype.classDescription,
contractID: component.prototype.contractID,
factory: this._getFactory(component),
categories: component.prototype._xpcom_categories
});
}
return { // nsIModule impl.
getClassObject: function(compMgr, cid, iid) {
// We only support nsIFactory queries, not nsIClassInfo
if (!iid.equals(Ci.nsIFactory))
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
for each (let classDesc in classes) {
if (classDesc.cid.equals(cid))
return classDesc.factory;
}
throw Cr.NS_ERROR_FACTORY_NOT_REGISTERED;
},
registerSelf: function(compMgr, fileSpec, location, type) {
var componentCount = 0;
debug("*** registering " + fileSpec.leafName + ": [ ");
compMgr.QueryInterface(Ci.nsIComponentRegistrar);
for each (let classDesc in classes) {
debug((componentCount++ ? ", " : "") + classDesc.className);
compMgr.registerFactoryLocation(classDesc.cid,
classDesc.className,
classDesc.contractID,
fileSpec,
location,
type);
if (classDesc.categories) {
let catMan = XPCOMUtils.categoryManager;
for each (let cat in classDesc.categories) {
let defaultValue = (cat.service ? "service," : "") +
classDesc.contractID;
catMan.addCategoryEntry(cat.category,
cat.entry || classDesc.className,
cat.value || defaultValue,
true, true);
}
}
}
if (postRegister)
postRegister(compMgr, fileSpec, componentsArray);
debug(" ]\n");
},
unregisterSelf: function(compMgr, fileSpec, location) {
var componentCount = 0;
debug("*** unregistering " + fileSpec.leafName + ": [ ");
compMgr.QueryInterface(Ci.nsIComponentRegistrar);
if (preUnregister)
preUnregister(compMgr, fileSpec, componentsArray);
for each (let classDesc in classes) {
debug((componentCount++ ? ", " : "") + classDesc.className);
if (classDesc.categories) {
let catMan = XPCOMUtils.categoryManager;
for each (let cat in classDesc.categories) {
catMan.deleteCategoryEntry(cat.category,
cat.entry || classDesc.className,
true);
}
}
compMgr.unregisterFactoryLocation(classDesc.cid, fileSpec);
}
debug(" ]\n");
},
canUnload: function(compMgr) {
return true;
}
};
},
/**
* Convenience access to category manager
*/
get categoryManager() {
return Components.classes["@mozilla.org/categorymanager;1"]
.getService(Ci.nsICategoryManager);
},
/**
* Returns an nsIFactory for |component|.
*/
_getFactory: function(component) {
var factory = component.prototype._xpcom_factory;
if (!factory) {
factory = {
createInstance: function(outer, iid) {
if (outer)
throw Cr.NS_ERROR_NO_AGGREGATION;
return (new component()).QueryInterface(iid);
}
}
}
return factory;
}
};
/**
* Helper for XPCOMUtils.generateQI to avoid leaks - see bug 381651#c1
*/
function makeQI(interfaceNames) {
return function XPCOMUtils_QueryInterface(iid) {
if (iid.equals(Ci.nsISupports))
return this;
for each(let interfaceName in interfaceNames) {
if (Ci[interfaceName].equals(iid))
return this;
}
throw Cr.NS_ERROR_NO_INTERFACE;
};
}
//@line 477 "/home/steve/dev/mozilla/1.8/mozilla/extensions/greasefire/src/gfGreasefireService.js"
gfGreasefireService.prototype.QueryInterface =
XPCOMUtils.generateQI([Ci.gfIGreasefireService,
Ci.nsIObserver]);
gfSearchResult.prototype.QueryInterface =
XPCOMUtils.generateQI([Ci.gfISearchResult]);
var NSGetModule = XPCOMUtils.generateNSGetModule(
[
gfGreasefireService
],
function(aCompMgr, aFileSpec, aLocation) {
XPCOMUtils.categoryManager.addCategoryEntry(
"app-startup",
gfGreasefireService.prototype.classDescription,
"service," + gfGreasefireService.prototype.contractID,
true,
true);
}
);